﻿#pragma once
#include<map>
#include<assert.h>
#include<iostream>
#include<vector>
using namespace std;
template<class K, class V>
struct AVLTreeNode
{
	// 需要parent指针，后续更新平衡因⼦可以看到
	pair<K, V> _kv;
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;
	int _bf; // balance factor
	AVLTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{}
};

template<class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	Node* Find(K key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}
	int Size()
	{
		return _size(_root);
	}
	int Height()
	{
		return _Height(_root);
	}
	bool IsBalanceTree()
	{
		 return _IsBalanceTree(_root);
	}
	bool Insert(const pair<K, V>& kv)
	{
		if (_root == NULL)
		{
			_root = new Node(kv);
			return true;
		}
		Node* parent=_root;
		Node* cur = _root;

		while (cur)
		{
			if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
				return false;
		}
		cur = new Node(kv);
		if (kv.first > parent->_kv.first)
		{
			parent->_right = cur;
		}
		else 
		{
			parent->_left = cur;
		}
		cur->_parent = parent;
		//更新平衡因子

		while (parent)
		{
			if (parent->_right == cur) parent->_bf++;
			else parent->_bf--;

			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				//继续向上更新平衡因子
				cur = parent;
				parent = cur->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				//需要看情况进行四种旋转
				if (parent->_bf == -2 && cur->_bf == -1)
				{
					//进行右单旋
					RoteR(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == 1)
				{
					//左右双旋
					RoteLR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == 1)
				{
					//进行左单旋
					RoteL(parent);
				}
				else if (parent->_bf == 2 || parent->_bf == -1)
				{
					//进行右左双旋
					RoteRL(parent);
				}
				else
				{
					assert(false);
				}
				//旋转之后已经平衡了退出循环
				break;
			}
			else
			{
				assert(false);
			}
		}
		return true;
	}

	void InOrder()
	{
		_Inorder(_root);
	}
private:
	int  _size(Node* root)
	{
		if (root == nullptr) return 0;
		return _size(root->_left) + _size(root->_right) + 1;
	}
	void RoteR(Node* parent)
	{
		Node* sub = parent;
		Node* subl = parent->_left;
		Node* sublr = subl->_right;

		Node* tem = sub->_parent;
		sub->_left = sublr;
		if (sublr) sublr->_parent = sub;

		subl->_right = sub;
		sub->_parent = subl;


		if (tem == nullptr)
		{
			_root = subl;
			subl->_parent = nullptr;
		}
		else
		{
			//与上面进行连接

			if (tem->_left == parent) tem->_left = subl;
			else tem->_right = subl;
			subl->_parent = tem;
		}
		
		
		//更新平衡因子
		sub->_bf = 0;
		subl->_bf = 0;
	}

	void RoteL(Node* parent)
	{
		Node* sub = parent;
		Node* subr = parent->_right;
		Node* subrl = subr->_left;

		Node* tem = sub->_parent;
		sub->_right = subrl;
		if (subrl) subrl->_parent = sub;

		subr->_left = sub;
		sub->_parent = subr;

		if (tem == nullptr)
		{
			_root = subr;
			subr->_parent = nullptr;
		}
		else
		{
			//与上面进行连接

			if (tem->_left == parent) tem->_left = subr;
			else tem->_right = subr;

			subr->_parent = tem;
		}
		//更新平衡因子
		sub->_bf = 0;
		subr->_bf = 0;
	}
	//左右双旋
	void RoteLR(Node* parent)
	{
		Node* sub = parent;
		Node* subl = parent->_left;
		Node* sublr = subl->_right;

		//此时sublr一定不为空因为至少在b树插入一个节点
		int bf = sublr->_bf;
		//记录一下插入之后sublr的平衡因子因为在后续左右单旋是会导致
		//旋转节点和他的左节点的平衡因子为0;
		RoteL(subl);
		RoteR(sub);

		//根据记录的bf来更新我们旋转之后的平衡因子
		if (bf == 0)
		{
			sub->_bf = 0;
			subl->_bf = 0;
			sublr->_bf = 0;
		}
		else if (bf == 1)
		{
			sub->_bf = 0;
			subl->_bf = -1;
			sublr->_bf = 0;
		}
		else if (bf == -1)
		{
			sub->_bf = 1;
			subl->_bf = 0;
			sublr->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
	//进行右左双旋
	void RoteRL(Node* parent)
	{
		Node* sub = parent;
		Node* subr = parent->_right;
		Node* subrl = subr->_left;

		//此时sublr一定不为空因为至少在b树插入一个节点
		int bf = subrl->_bf;
		//记录一下插入之后sublr的平衡因子因为在后续左右单旋是会导致
		//旋转节点和他的左节点的平衡因子为0;
		RoteR(subr);
		RoteL(sub);

		//根据记录的bf来更新我们旋转之后的平衡因子
		if (bf == 0)
		{
			sub->_bf = 0;
			subr->_bf = 0;
			subrl->_bf = 0;
		}
		else if (bf == 1)
		{
			sub->_bf = -1;
			subr->_bf = 0;
			subrl->_bf = 0;
		}
		else if (bf == -1)
		{
			sub->_bf = 0;
			subr->_bf = 1;
			subrl->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
	void _Inorder(Node* root)
	{
		if (root == NULL) return;
		_Inorder(root->_left);
		cout << root->_kv.first <<":"<< root->_kv.second << endl;
		_Inorder(root->_right);
	}
	bool _IsBalanceTree(Node* root)
	{
		// 空树也是AVL树
		if (nullptr == root)
			return true;
		// 计算pRoot结点的平衡因⼦：即pRoot左右⼦树的⾼度差
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		int diff = rightHeight - leftHeight;
		// 如果计算出的平衡因⼦与pRoot的平衡因⼦不相等，或者
// pRoot平衡因⼦的绝对值超过1，则⼀定不是AVL树
		if (abs(diff) >= 2)
		{
			cout << root->_kv.first << "高度差异常" << endl;
			return false;
		}
		if (root->_bf != diff)
		{
			cout << root->_kv.first << "平衡因子" << endl;
			return false;
		}
		// pRoot的左和右如果都是AVL树，则该树⼀定是AVL树
		return _IsBalanceTree(root->_left) && _IsBalanceTree(root->_right);
	}
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	Node* _root = nullptr;
};